# Copyright 2009 Michal Trzcinka
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

When /^I delete grid "([^\"]*)"$/ do |name|
  row = @browser.table(:id, 'grids').rows.find {|r| r[1].text == name }
  row[6].link(:text, 'Delete').click
  @browser.button(:value, 'Yes').click
end

Given /^I tried to create a grid named "([^\"]*)" with "([^\"]*)" description, "([^\"]*)" rows and "([^\"]*)" columns$/ do |name, description, rows, columns|
  Given "I am on the new grid form"
  When "I fill text field \"grid_name\" with \"#{name}\""
  When "I fill text field \"grid_description\" with \"#{description}\""
  When "I fill text field \"grid_rows\" with \"#{rows}\""
  When "I fill text field \"grid_columns\" with \"#{columns}\""
  When "I click button \"Create\""
end

Then /^I should see a new grid form with name, description and dimensions$/ do
  form = @browser.form(:id, 'new_grid')
  form.should exist
  ['name', 'description', 'columns', 'rows'].each do |el|
    @browser.text_field(:id, 'grid_' + el).should exist
  end
  @browser.button(:value, 'Create').should exist
end

Then /^I should see a grid panel$/ do
  @browser.div(:id, 'vcard').should exist
end

Then /^I should not see a grid panel$/ do
  @browser.div(:id, 'vcard').should_not exist
end

Then /^I should see an empty list of grids$/ do
  Then "I should see \"Grids list\""
  @browser.table(:id, 'grids').rows.length.should == 1
end


Then /^I should see only a grid named "([^\"]*)" with "([^\"]*)" rows and "([^\"]*)" columns on the grids list$/ do |name, rows, columns|
  Then "I should see \"Grids list\""
  table = @browser.table(:id, 'grids')
  table.rows.length.should == 2
  row = table.rows[1]
  row[1].text.should == name
  row[2].text.should == rows
  row[3].text.should == columns
  row[4].text.should == 'Show'
  row[5].text.should == 'Edit'
  row[6].text.should == 'Delete'
end
